#import "SMRShadowImageView.h"
#import "SMROverlayShadowEffectView.h"
#import "SMRShadowImageCell.h"
#import "SMRBorderFrame.h"


#pragma mark Constants
static const CGFloat SMRShadowImageDefaultShadowBorderPadding = 6.0f;


#pragma mark -
@interface SMRShadowImageView ()
@property (nonatomic, assign) BOOL overlayRectNeedsUpdate;
@end


#pragma mark -
@interface SMRShadowImageView (SMRImageViewPrivateMethods)
-(void)shadowImageViewCommonInit_;
-(SMRShadowImageCell *)shadowCell_;
-(void)updateOverlay;
-(NSRect)overlayRectForRect:(NSRect)bounds;
@end


#pragma mark -
@implementation SMRShadowImageView

@synthesize shadowView;
@synthesize overlayRectNeedsUpdate;


+(Class)cellClass {
	return [SMRShadowImageCell class];
}

#pragma mark -
#pragma mark Initialization

-(id)initWithFrame:(NSRect)frameRect {
	if (!(self = [super initWithFrame:frameRect]))
		return nil;
	[self shadowImageViewCommonInit_];
	return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {
	if (!(self = [super initWithCoder:aDecoder]))
		return nil;
	[self shadowImageViewCommonInit_];
	return self;
}

-(void)shadowImageViewCommonInit_ {
	[self setShadowView:[[SMROverlayShadowEffectView alloc] initWithFrame:NSZeroRect]];
	[[self shadowView] setBorderPadding:SMRShadowImageDefaultShadowBorderPadding];
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shadowImageViewDidChangeFrame:) name:NSViewFrameDidChangeNotification object:self];
}


#pragma mark -
#pragma mark Notifications

-(void)shadowImageViewDidChangeFrame:(NSNotification *)note {
	if ([[self shadowView] superview])
		[self setOverlayRectNeedsUpdate:YES];
}


#pragma mark -
#pragma mark Border frame

-(SMRBorderFrame *)borderFrame {
	return [[self shadowCell_] borderFrame];
}

-(void)setBorderFrame:(SMRBorderFrame *)frame {
	[[self shadowCell_] setBorderFrame:frame];
	[self setNeedsDisplay:YES];
}


#pragma mark -
#pragma mark Drawing

-(void)viewWillDraw {
	if ([self overlayRectNeedsUpdate])
		[self updateOverlay];
}

-(void)drawRect:(NSRect)dirtyRect {
	if ([[self shadowView] superview]) {
		[[NSColor whiteColor] setFill];
		NSRect overlayRect = [self overlayRectForRect:[self bounds]];
		[NSBezierPath fillRect:overlayRect];
	}
	[super drawRect:[self bounds]];	
}


#pragma mark -
#pragma mark First responder

-(BOOL)acceptsFirstResponder {
	return YES;
}

-(BOOL)becomeFirstResponder {
	if ([self isEditable]) 
		[self showEditModeBezel];
	return [super becomeFirstResponder];
}

-(BOOL)resignFirstResponder {
	[self hideEditModeBezel];
	return [super resignFirstResponder];
}


#pragma mark -
#pragma mark Event handling

-(void)mouseDown:(NSEvent *)theEvent {
//	NSResponder *firstResponder = [[self window] firstResponder];
//	if (firstResponder == [self shadowView])
//		return;
	if ([self isEditable])
		[[self window] makeFirstResponder:self];
}


#pragma mark -
#pragma mark Overlay support

-(void)showEditModeBezel {
	[self setOverlayRectNeedsUpdate:YES];
	[self setNeedsDisplay:YES];
}

-(void)hideEditModeBezel {
	[[self shadowView] removeFromSuperview];
	[self setNeedsDisplay:YES];
}

-(NSRect)overlayRectForRect:(NSRect)bounds {
	NSImage *image = [self objectValue];
	if (!image)
		return bounds;
//	SMRBorderFrame *borderFrame = [self borderFrame];
//	NSRect imageRect = (borderFrame == nil) ? bounds : [borderFrame imageRectForFrameRect:bounds];
//	CGFloat horizontalPadding = bounds.size.width - imageRect.size.width;
//	NSSize imageSize = [image size];
//	CGFloat widthRatio = imageSize.width / imageSize.height;
//	CGFloat overlayWidth = imageRect.size.width * widthRatio;
//	NSRect overlayRect = bounds;
//	overlayRect.size.width = overlayWidth;
	NSRect overlayRect = bounds;
	overlayRect.size = [[self cell] cellSizeForBounds:bounds];
	return overlayRect;
}

-(void)updateOverlay {
	NSRect overlayRect = [self overlayRectForRect:[self bounds]];
	[self setOverlayRectNeedsUpdate:NO];
	[[self shadowView] setOverlayRect:overlayRect inView:self];
}


#pragma mark -
#pragma mark Private

-(SMRShadowImageCell *)shadowCell_ {
	return (SMRShadowImageCell *)[self cell];
}

@end
